package aa.jx.order.controller;

import aa.jx.order.model.Order;
import aa.jx.order.model.OrderStatistics;
import aa.jx.order.model.PageResult;
import aa.jx.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/admin/orders")
public class AdminOrderController {

    @Autowired
    private OrderService orderService;

    // 获取所有订单（可分页、筛选）
    @GetMapping
    public PageResult<Order> getAllOrders(
            @RequestParam(required = false) String orderNo,
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return orderService.getAllOrders(orderNo, userId, status, startTime, endTime, page, size);
    }

    // 获取订单详情
    @GetMapping("/{orderId}")
    public Order getOrderDetail(@PathVariable Long orderId) {
        return orderService.getAdminOrderDetail(orderId);
    }

    // 发货操作
    @PutMapping("/{orderId}/ship")
    public void shipOrder(@PathVariable Long orderId,
                          @RequestParam String shippingCompany,
                          @RequestParam String shippingNo) {
        orderService.shipOrder(orderId, shippingCompany, shippingNo);
    }

    // 取消订单
    @PutMapping("/{orderId}/cancel")
    public void cancelOrder(@PathVariable Long orderId) {
        orderService.cancelOrder(orderId);
    }

    // 订单统计
    @GetMapping("/statistics")
    public OrderStatistics getOrderStatistics(
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        return orderService.getOrderStatistics(startTime, endTime);
    }
    @PutMapping("/{orderId}/status")
    public void updateOrderStatus(@PathVariable Long orderId,
                                  @RequestParam Integer status,
                                  @RequestParam(required = false) String shippingCompany,
                                  @RequestParam(required = false) String shippingNo) {
        orderService.updateOrderStatus(orderId, status, shippingCompany, shippingNo);
    }
}